<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <page-filter
      v-model:query="filterInfo.query"
      :clear-type="clearType"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
    </page-filter>
    <!-- 条件栏 -->
    <DialogTable
      :refresh="tableInfo.refresh"
      :init-curpage="tableInfo.initCurpage"
      :api="tableInfo.api"
      :query="filterInfo.query"
      v-model:data="tableInfo.data"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-qty="scope">
        {{ $fn.Subtr(scope.row.qty, scope.row.receiveQty || 0) }}
      </template>
    </DialogTable>
  </page-dialog>
</template>
<script setup name="selectContractStore">
import PageDialog from '@/components/PageDialog'
import PageFilter from '@/components/NewPageFilter'
import DialogTable from '@/components/DialogTable'
import { getSupplierAll } from '@/api/core'
import { getPendDealContractPage } from '@/api/contract'

const { proxy } = getCurrentInstance()
const emits = defineEmits()

const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: true,
  },
  checkBox: {
    type: Boolean,
    default: false,
  },
  visible: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
})

const filterInfo = reactive({
  copyQuery: {},
  query: {
    // purchProductType: "2",
    // supplierAuth: "supplierId"
  },
  searchList: [
    {
      componentType: 'input',
      label: '合同单号',
      value: 'orderNoLike',
      event: search,
    },
    {
      componentType: '供应商',
      value: 'supplierIdMult',
      type: 'slip-select',
      api: getSupplierAll,
      query: { supplierAuth: 'supplierId', sort: 'create_time desc' },
      clearable: true,
      filterable: true,
      keyField: 'supplierName',
      valueField: 'supplierId',
      event: search,
    },
  ],
  searchBtn: [
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '重置', event: clearSearch },
  ],
})
let clearType = reactive(true)
const tableInfo = reactive({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: getPendDealContractPage,
  fieldList: [
    { label: '合同单号', value: 'orderNo' },
    { label: '合同名称', value: 'contractName' },
    { label: '状态', value: 'statusValue' },
    { label: '备注', value: 'remark' },
  ],
})
const dialogInfo = reactive({
  title: '选择合同',
  width: '80%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    { label: '保存', event: handleSave, disabled: true, show: true },
  ],
})
let selectRow = reactive({})

let shows = computed({
  get() {
    return props.visible
  },
  set(newValue) {
    emits('update:visible', newValue) // 更新父组件shows
  },
})

setData()
function setData() {
  filterInfo.query = {
    ...filterInfo.query,
    ...props.query,
  }
  filterInfo.copyQuery = proxy.$fn.deepClone(filterInfo.query)
  getList()
}

function getList() {
  proxy.$nextTick(() => {
    tableInfo.refresh = Math.random()
  })
}
function search() {
  proxy.$nextTick(() => {
    tableInfo.initCurpage = Math.random()
    tableInfo.refresh = Math.random()
  })
}

function handleEvent(event, data) {
  if (event === 'tableClickRow') {
    if (data && data.id) {
      selectRow = data
      dialogInfo.btList[1].disabled = false
    }
  }
}
function handleSave() {
  shows.value = false
  emits('callback', selectRow)
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
}
function clearSearch() {
  filterInfo.query = proxy.$fn.deepClone(filterInfo.copyQuery)
  clearType = !clearType
  getList()
}
</script>
<style scoped lang="scss"></style>
